package cn.fzkj.framework.security.handle;

import cn.fzkj.common.constant.HttpStatus;
import cn.fzkj.common.utils.ServletUtils;
import cn.fzkj.common.utils.StringUtils;
import cn.fzkj.framework.web.domain.ResultVO;
import com.alibaba.fastjson.JSON;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/** 权限不足 */
@Component
public class AcessDeniedServletHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        int code = HttpStatus.FORBIDDEN;
        String msg = StringUtils.format("请求访问：{}，AccessDenied，没有权限", request.getRequestURI());
        ServletUtils.renderString(response, JSON.toJSONString(ResultVO.error(code, msg)));
    }
}
